package com.xudow.app.dynamicstate_old.module.follow;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.MimeTypeMap;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.utils.JUtils;
import com.squareup.picasso.Picasso;
import com.umeng.share.ShareManager;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.JTimeTransform;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.user.UserProfileActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zhuchenxi on 16/6/26.
 */

public class DynamicLongDetailActivity extends BeamBaseActivity {
    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.image_share)
    ImageView imageShare;
    @BindView(R.id.photo)
    CircleImageView photo;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.text_info)
    TextView textInfo;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    //    @BindView(R.id.img_collection)
//    ImageView imgCollect;
    private Dynamic dynamic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dynamic = (Dynamic) getIntent().getSerializableExtra("dynamic");
        setContentView(R.layout.activity_dynamic_long_detail);
        ButterKnife.bind(this);
        tvTitle.setText(dynamic.getTitle());
        imageBack.setOnClickListener(v -> finish());
        imageShare.setOnClickListener(v -> share());
        photo.setOnClickListener(v -> {
            Intent i = new Intent(this, UserProfileActivity.class);
            i.putExtra(UserProfileActivity.USER_PROFILE_ID, dynamic.getUserProfileId());
            startActivity(i);
        });

        Picasso.with(this).load(ImageModel.getXueDouImage(dynamic.getHeadUrl())).placeholder(R.drawable.default_head_img).into(photo);
        textName.setText(dynamic.getUserName());
        DynamicModel.getInstance().readArticleCount(dynamic.getId())
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.NONE))
                .subscribe();
        textInfo.setText(new JTimeTransform(dynamic.getPublishTime()).toString(new JTimeTransform.RecentDateFormat()) + " " + dynamic.getReadnum() + "次阅读");
//        if (dynamic.getIsCollected() == 1) {
//            imgCollect.setImageResource(R.drawable.collected_dynamic);
//        } else {
//            imgCollect.setImageResource(R.drawable.collect);
//        }
//        imgCollect.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (dynamic.getIsCollected() == 0)
//                    collect(imgCollect);
//                else
//                    unCollect(imgCollect);
//            }
//        });
        webview.loadUrl(Config.BASE_URL + dynamic.getUrl());
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                String mimeType = MimeTypeMap.getFileExtensionFromUrl(url);
                if (mimeType.equals("docx") || mimeType.equals("ppt") || mimeType.equals("pptx") || mimeType.equals("doc") || mimeType.equals("mp3")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
    }

//    private void collect(final ImageView imgCollect) {
//        DynamicModel.getInstance().collectDynamic(dynamic.getId())
//                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
//                .subscribe(message -> {
//                    dynamic.setIsCollected(1);
//                    imgCollect.setImageResource(R.drawable.collected_dynamic);
//                });
//    }
//
//    private void unCollect(final ImageView imgCollect) {
//        DynamicModel.getInstance().unCollectDynamic(dynamic.getId())
//                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
//                .subscribe(message -> {
//                    dynamic.setIsCollected(0);
//                    imgCollect.setImageResource(R.drawable.collect);
//                });
//    }

    public void share() {
        String content = dynamic.getContent();

        ShareManager.getInstance(this).shareContent(this, content, Config.BASE_URL + dynamic.getUrl() + "?isshow=1", "学兜分享", dynamic.getImgpath()
                , new UMShareListener() {
                    @Override
                    public void onResult(SHARE_MEDIA platform) {
                        Toast.makeText(DynamicLongDetailActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA platform, Throwable t) {
                        JUtils.Log(t.getMessage());
                        Toast.makeText(DynamicLongDetailActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(DynamicLongDetailActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
